﻿#region Copyright (c) 2013-06, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

#if TESTS

using System;
using System.Threading.Tasks;
using System.Windows;
using Amarok.Agents;
using Amarok.Presentation.Resources;
using NUnit.Framework;


namespace Amarok.Presentation
{
	[TestFixture]
	public class Test_DefaultControllerFactory
	{
		private class StubViewModel : ViewModel
		{
		}

		[MessageBusConnected]
		private class MockController : Controller<StubViewModel>
		{
			public MockController(AgentEnvironment environment, AgentOptions options, IView view, StubViewModel viewModel)
				: base(environment, options, view, viewModel)
			{
			}

			public new AgentEnvironment Environment
			{
				get
				{
					return base.Environment;
				}
			}

			public new AgentOptions Options
			{
				get
				{
					return base.Options;
				}
			}

			public new IView View
			{
				get
				{
					return base.View;
				}
			}
		}

		private class MockAgentWithoutMessageBusConnectedAttribute : Controller<StubViewModel>
		{
			public MockAgentWithoutMessageBusConnectedAttribute(AgentEnvironment environment, AgentOptions options, IView view, StubViewModel viewModel)
				: base(environment, options, view, viewModel)
			{
			}

			public new AgentEnvironment Environment
			{
				get
				{
					return base.Environment;
				}
			}

			public new AgentOptions Options
			{
				get
				{
					return base.Options;
				}
			}

			public new IView View
			{
				get
				{
					return base.View;
				}
			}
		}

		private class ControllerWithInvalidConstructor : Controller<StubViewModel>
		{
			public ControllerWithInvalidConstructor()
				: base(null, null, null, null)
			{
			}
		}

		private class ControllerWithExceptionInConstructor : Controller<StubViewModel>
		{
			public ControllerWithExceptionInConstructor(AgentEnvironment environment, AgentOptions options, IView view, StubViewModel viewModel)
				: base(environment, options, view, viewModel)
			{
				throw new ApplicationException("CTR");
			}
		}

		private class StubEnvironment : AgentEnvironment
		{
		}

		private class StubView : IView
		{
			public Object ViewModel
			{
				get;
				set;
			}

			public FrameworkElement Control
			{
				get
				{
					throw new NotImplementedException();
				}
			}

			public void Invoke(Action action)
			{
				throw new NotImplementedException();
			}

			public Task InvokeAsync(Action action)
			{
				throw new NotImplementedException();
			}

			public void Send(ViewAction viewAction)
			{
				throw new NotImplementedException();
			}

			public Task SendAsync(ViewAction viewAction)
			{
				throw new NotImplementedException();
			}
		}

		private class StubOptions : AgentOptions
		{
		}

		private class MockMessageBus : IMessageBus
		{
			public Int32 ConnectCalled;
			public IAgent ConnectArgument;

			public ICompletable Connect(IAgent controller)
			{
				ConnectArgument = controller;
				ConnectCalled++;

				return new DummyCompletable();
			}

			public ICompletable Connect(Object label, IAgent controller)
			{
				throw new NotImplementedException();
			}

			public Task Completion
			{
				get
				{
					throw new NotImplementedException();
				}
			}

			public void Complete(bool discardAvailableMessages = false)
			{
				throw new NotImplementedException();
			}

			public void Dispose()
			{
				throw new NotImplementedException();
			}

			public ICompletable Subscribe(Type messageType, AgentReference subscriber, Predicate<Message> messageFilter = null)
			{
				throw new NotImplementedException();
			}

			public ICompletable Subscribe(Type messageType, Action<Message> subscriber, Predicate<Message> messageFilter = null)
			{
				throw new NotImplementedException();
			}

			public void Publish(Message message)
			{
				throw new NotImplementedException();
			}
		}

		private class StubRuntimeEnvironment : RuntimeEnvironment
		{
			public StubRuntimeEnvironment(IRuntime runtime, IMessageBus messageBus)
				: base(runtime, messageBus)
			{
			}
		}


		// --- TESTS ---


		#region SpawnController(Type, IView, Object, AgentEnvironment, AgentOptions)

		[Test]
		public void Test_SpawnController_ViewModel()
		{
			var environment = new StubEnvironment();
			var options = new StubOptions();
			var view = new StubView();
			var viewModel = new StubViewModel();
			var factory = new DefaultControllerFactory();

			var controller = (MockController)factory.SpawnController(
				typeof(MockController),
				view,
				viewModel,
				environment,
				options);

			Assert.That(controller, Is.Not.Null);
			Assert.That(controller.Environment, Is.SameAs(environment));
			Assert.That(controller.Options, Is.SameAs(options));
			Assert.That(controller.View, Is.SameAs(view));
			Assert.That(controller.ViewModel, Is.SameAs(viewModel));
		}

		[Test]
		public void Test_SpawnController_ViewModel_NullType()
		{
			var environment = new StubEnvironment();
			var options = new StubOptions();
			var view = new StubView();
			var viewModel = new StubViewModel();
			var factory = new DefaultControllerFactory();

			Assert.Throws<ArgumentNullException>(() =>
			{
				var controller = factory.SpawnController(
					null,
					view,
					viewModel,
					environment,
					options);
			});
		}

		[Test]
		public void Test_SpawnController_ViewModel_NullView()
		{
			var viewModel = new StubViewModel();
			var environment = new StubEnvironment();
			var options = new StubOptions();
			var factory = new DefaultControllerFactory();

			Assert.Throws<ArgumentNullException>(() =>
			{
				var controller = factory.SpawnController(
					typeof(MockController),
					null,
					viewModel,
					environment,
					options);
			});
		}

		[Test]
		public void Test_SpawnController_ViewModel_NullViewModel()
		{
			var view = new StubView();
			var environment = new StubEnvironment();
			var options = new StubOptions();
			var factory = new DefaultControllerFactory();

			Assert.Throws<ArgumentNullException>(() =>
			{
				var controller = factory.SpawnController(
					typeof(MockController),
					view,
					null,
					environment,
					options);
			});
		}

		[Test]
		public void Test_SpawnController_ViewModel_NullEnvironment()
		{
			var view = new StubView();
			var viewModel = new StubViewModel();
			var options = new StubOptions();
			var factory = new DefaultControllerFactory();

			Assert.Throws<ArgumentNullException>(() =>
			{
				var controller = factory.SpawnController(
					typeof(MockController),
					view,
					viewModel,
					null,
					options);
			});
		}

		[Test]
		public void Test_SpawnController_ViewModel_NullOptions()
		{
			var view = new StubView();
			var viewModel = new StubViewModel();
			var environment = new StubEnvironment();
			var factory = new DefaultControllerFactory();

			Assert.Throws<ArgumentNullException>(() =>
			{
				var controller = factory.SpawnController(
					typeof(MockController),
					view,
					viewModel,
					environment,
					null);
			});
		}

		[Test]
		public void Test_SpawnController_ViewModel_NonAgentType()
		{
			var view = new StubView();
			var viewModel = new StubViewModel();
			var environment = new StubEnvironment();
			var options = new StubOptions();
			var factory = new DefaultControllerFactory();

			Assert.Throws<ArgumentException>(() =>
			{
				var controller = factory.SpawnController(
					typeof(Version),
					view,
					viewModel,
					environment,
					options);
			});
		}

		[Test]
		public void Test_SpawnController_ViewModel_TypeWithInvalidConstructor()
		{
			var view = new StubView();
			var viewModel = new StubViewModel();
			var environment = new StubEnvironment();
			var options = new StubOptions();
			var factory = new DefaultControllerFactory();

			var exception = Assert.Throws<ControllerSpawnException>(() =>
			{
				var controller = factory.SpawnController(
					typeof(ControllerWithInvalidConstructor),
					view,
					viewModel,
					environment,
					options);
			});

			Assert.That(exception.Message, Is.EqualTo(ExceptionResources.ControllerTypeDoesNotProvideControllerConstructor));
		}

		[Test]
		public void Test_SpawnController_ViewModel_TypeWithExceptionInConstructor()
		{
			var view = new StubView();
			var viewModel = new StubViewModel();
			var environment = new StubEnvironment();
			var options = new StubOptions();
			var factory = new DefaultControllerFactory();

			var exception = Assert.Throws<ControllerSpawnException>(() =>
			{
				var controller = factory.SpawnController(
					typeof(ControllerWithExceptionInConstructor),
					view,
					viewModel,
					environment,
					options);
			});

			Assert.That(exception.Message, Is.EqualTo(ExceptionResources.ControllerSpawnFailure));
		}

		[Test]
		public void Test_SpawnController_ViewModel_RuntimeEnvironment()
		{
			var runtime = new DummyRuntime();
			var messageBus = new MockMessageBus();
			var view = new StubView();
			var viewModel = new StubViewModel();
			var environment = new StubRuntimeEnvironment(runtime, messageBus);
			var options = new StubOptions();
			var factory = new DefaultControllerFactory();

			var controller = (MockController)factory.SpawnController(
				typeof(MockController),
				view,
				viewModel,
				environment,
				options);

			Assert.That(controller, Is.Not.Null);
			Assert.That(controller.Environment, Is.SameAs(environment));
			Assert.That(controller.Options, Is.SameAs(options));
			Assert.That(controller.View, Is.SameAs(view));
			Assert.That(controller.ViewModel, Is.SameAs(viewModel));
			Assert.That(messageBus.ConnectCalled, Is.EqualTo(1));
			Assert.That(messageBus.ConnectArgument, Is.SameAs(controller));
		}

		[Test]
		public void Test_SpawnController_ViewModel_RuntimeEnvironment_AgentWithoutMessageBusConnectedAttribute()
		{
			var runtime = new DummyRuntime();
			var messageBus = new MockMessageBus();
			var view = new StubView();
			var viewModel = new StubViewModel();
			var environment = new StubRuntimeEnvironment(runtime, messageBus);
			var options = new StubOptions();
			var factory = new DefaultControllerFactory();

			var controller = (MockAgentWithoutMessageBusConnectedAttribute)factory.SpawnController(
				typeof(MockAgentWithoutMessageBusConnectedAttribute),
				view,
				viewModel,
				environment,
				options);

			Assert.That(controller, Is.Not.Null);
			Assert.That(controller.Environment, Is.SameAs(environment));
			Assert.That(controller.Options, Is.SameAs(options));
			Assert.That(controller.View, Is.SameAs(view));
			Assert.That(controller.ViewModel, Is.SameAs(viewModel));
			Assert.That(messageBus.ConnectCalled, Is.EqualTo(0));
		}

		#endregion

	}
}

#endif
